<template>
	<view class="container"><yxylInputs :inputs="inputs" @confirm="confirm"></yxylInputs></view>
</template>

<script>
import yxylInputs from '@/components/yxyl-inputs/yxyl-inputs.vue';
export default {
	components: { yxylInputs },
	data() {
		return {
			inputs: [],
			communityId: ''
		};
	},
	onLoad(e) {
		if (e.scene) {
			this.communityId = e.scene;
		} else {
			this.communityId = e.CommunityId;
		}
		this.$http
			.request({
				url: 'api/Community/GetCommunity',
				data: {
					Id: this.communityId
				}
			})
			.then(res => {
				// #ifdef H5
				this.$wechat.hideMenu();
				// #endif
				this.inputs.push({ title: '社区名称', key: 'communityName', placeholder: '请输入社区名称', required: true, type: 'text', errMsg: '请输入社区名称' });
				if (res.communityName) {
					this.inputs[0].default = res.communityName;
				}
				this.inputs.push({ title: '省市区', keys: ['provinceId', 'cityId', 'areaId'], type: 'address', level: 3 });
				if (res.provinceId) {
					this.inputs[1].default = [res.provinceId, res.cityId, res.areaId];
				}
				this.inputs.push({ title: '详细地址', key: 'address', placeholder: '请输入详细地址', type: 'text', errMsg: '请输入详细地址' });
				if (res.address) {
					this.inputs[2].default = res.address;
				}
				this.inputs.push({ title: '联系人', key: 'contacts', placeholder: '请输入联系人', required: true, type: 'text', errMsg: '请输入联系人' });
				if (res.contacts) {
					this.inputs[3].default = res.contacts;
				}
				this.inputs.push({ title: '电话号码', key: 'telephone', placeholder: '请输入电话号码', required: true, type: 'number', errMsg: '请输入电话号码' });
				if (res.telephone) {
					this.inputs[4].default = res.telephone;
				}
			});
	},
	methods: {
		confirm(e) {
			e.id = this.communityId;
			this.$http
				.request({
					url: 'api/Community/UpdateCommunityByAdmin',
					method: 'POST',
					data: e
				})
				.then(() => {
					uni.showToast({
						icon: 'success',
						title: '保存成功',
						success() {
							setTimeout(function() {
								if (getCurrentPages().length > 1) {
									uni.navigateBack()
								} else {
									uni.redirectTo({
										url: '../list'
									});
								}
							}, 1500);
						}
					});
				});
		}
	}
};
</script>

<style lang="scss" scoped></style>
